//实现桌面弹球
//用css的top和left改变图片的位置
	//生成一个随机数来决定小球的数量（1~5）
	var Ballnum = Math.floor(Math.random()*5+1);
							//console.log(Ballnum);
	//存放(X,Y)随机数数组
	var Ball_x=[];
	var Ball_y=[];
	//小球数组
	var Balls=[];
$(document).ready(function(){
	for (var i = 0; i < Ballnum; i++) {
		$(".container").append("<img src='images/点-1.png' class='ball'/>");
	$(".ball").each(function(){
		$(this).attr("id",$(this).index());
						//console.log($(this).val());
						//现在每一个球都有id了……
	});	
	//让每个小球刷新的位置不同	
		var randomNum_x = parseInt(Math.random()*400)+1;  //范围不会超过容器的长宽
		var randomNum_y = parseInt(Math.random()*200)+1;
		if(Ball_x.indexOf(randomNum_x)==-1 && Ball_y.indexOf(randomNum_y)==-1){
			//都不许重复
			Ball_x.push(randomNum_x);
			Ball_y.push(randomNum_y);
		}else{ //重复就再生成一次
			i--;
		}
		$("img:eq("+i+")").css("left",Ball_x[i]+"px");
		$("img:eq("+i+")").css("top",Ball_y[i]+"px");
		//console.log(Ball_x);
		//console.log(i);
		//console.log($("img:eq("+i+")").css("left"));这样获取的是一个字符串
		/*var ball ={
			img:$("img:eq("+i+")"),
			leftNum : 5,
            topNum : 5};*/
         var ball={          //小球对象，加入了独立的leftNum和topNum这样就不会同步啦！！！
         		img:$("img:eq("+i+")"),
         		leftNum:5,
				topNum:5,
     				};
		//console.log(ball.css("left")); 是带px的字符串
		//console.log(ball.position().left); //是完美的数字！！！
		//console.log(ball.offset().left);←生成的并不是left
		Balls.push(ball);
		//console.log(Balls[i]);
		//console.log(Balls[i].leftNum)

	}
	//$("img:eq(0)").css("left",Ball_x[0]+"px");
	//让每个小球朝一个方向运动，碰到边缘就"弹回"
	var leftMax=400-15;       //（容器的长宽-小球自身像素）
	var topMax=200-15;
	var Left=0;
	var Top=0;
	var leftNum=5;
	var topNum=5;
	/*不能设置统一的leftNum和topNum
	var leftNum=5; //每次移动5像素
	var topNum=5;*/
	
	function moveBall(ballObj){
		ballObj.timer=setInterval(function(){
		//console.log(ballObj.position().left);
		//...为什么会同步呢————因为每个球的leftNum和topNum都不同……
		 Left = ballObj.img.position().left+ballObj.leftNum;
		 Top  = ballObj.img.position().top+ballObj.topNum;
			if(Left>=leftMax){
				Left=leftMax;
				ballObj.leftNum=-ballObj.leftNum;
				ballObj.img.attr("src","images/点-2.png");
				setTimeout(function(){
				ballObj.img.attr("src","images/点-1.png");},200);


			}else if(Left<=0){
				Left = 0;
				ballObj.leftNum=-ballObj.leftNum;
				ballObj.img.attr("src","images/点-2.png");
				setTimeout(function(){
				ballObj.img.attr("src","images/点-1.png");},200);

			}
			if(Top>=topMax){
				Top=topMax;
				ballObj.topNum=-ballObj.topNum;
				ballObj.img.attr("src","images/点-2.png");
				setTimeout(function(){
				ballObj.img.attr("src","images/点-1.png");},200);
			}else if(Top<=0){
				Top=0;
				ballObj.topNum=-ballObj.topNum;
				ballObj.img.attr("src","images/点-2.png");
				setTimeout(function(){
				ballObj.img.attr("src","images/点-1.png");},200);

			}
			//console.log(Left);
		ballObj.img.css("left",Left+"px");
		ballObj.img.css("top",Top+"px");
	},100);
	}
	//console.log(Balls.length);
	for (var i = 0; i < Balls.length; i++) {
		moveBall(Balls[i]);
		//console.log(Balls[i]);
	}
	

});//结束ready	